package com.xzj.admin.resource.sync.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xzj.common.core.exception.ServiceException;
import com.xzj.common.id.gen.DefaultIdHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.xzj.common.datasource.domain.PageReq;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.common.datasource.utils.XzjPageUtil;
import com.xzj.admin.resource.sync.domain.entity.ResMessageSource;
import com.xzj.admin.resource.sync.mapper.ResMessageSourceMapper;
import com.xzj.admin.resource.sync.service.ResMessageSourceService;

import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: ResMessageSourceServiceImpl
 * @description: 资源消息源;(res_message_source)表服务实现类
 * @author: XiaZunJun
 * @create: 2024-10-31
 */
@Service
public class ResMessageSourceServiceImpl implements ResMessageSourceService{
    @Resource
    private ResMessageSourceMapper resMessageSourceMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param sourceId 主键
     * @return 实例对象
     */
    public ResMessageSource queryById(String sourceId){
        return resMessageSourceMapper.queryById(sourceId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    public PageResp<ResMessageSource> pageQuery(PageReq<ResMessageSource> pageReq){
        return XzjPageUtil.queryPage(pageReq, resMessageSource -> resMessageSourceMapper.pageQuery(resMessageSource));
    }


    @Override
    public List<ResMessageSource> queryAllByCloud(ResMessageSource resMessageSource) {
        return resMessageSourceMapper.pageQuery(resMessageSource);
    }

    /**
     * 新增数据
     *
     * @param resMessageSource 实例对象
     * @return 实例对象
     */
    public ResMessageSource insert(ResMessageSource resMessageSource){
        if(StrUtil.isEmpty(resMessageSource.getCloudOption())){
            throw new ServiceException("500","云资源配置项不允许修改");
        }
        String[] split = resMessageSource.getCloudOption().split(":");
        resMessageSource.setCloudId(split[0]);
        resMessageSource.setCloudType(split[1]);
        resMessageSource.setSourceId(DefaultIdHelper.nextIdStr());
        resMessageSourceMapper.insert(resMessageSource);
        return resMessageSource;
    }

    /**
     * 更新数据
     *
     * @param resMessageSource 实例对象
     * @return 实例对象
     */
    public ResMessageSource update(ResMessageSource resMessageSource){
        resMessageSourceMapper.update(resMessageSource);
        return resMessageSource;
    }

    /**
     * 通过主键删除数据
     *
     * @param sourceId 主键
     * @return 是否成功
     */
    public boolean deleteById(String sourceId){
        int total = resMessageSourceMapper.deleteById(sourceId);
        return total > 0;
    }
}